package com.citywy.service.impl;

import com.citywy.exception.BusinessException;
import com.citywy.controller.Code;
import com.citywy.exception.SystemException;
import com.citywy.dao.BookDao;
import com.citywy.domain.Book;
import com.citywy.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    //此处报错是因为BookDao是一个接口，实际注入的代理对象会在IOC容器创建时注入，当前未检测到报错，不影响运行
    @Autowired
    private BookDao bookDao;

    @Override
    public boolean save(Book book) {
        bookDao.save(book);
        return true;
    }

    @Override
    public boolean update(Book book) {
        bookDao.update(book);
        return true;
    }

    @Override
    public boolean delete(Integer id) {
        bookDao.delete(id);
        return true;
    }

    @Override
    public Book getById(Integer id) {
        //模拟业务异常，包装成自定义异常
        /*if(id==1){
            throw new BusinessException(Code.BUSINESS_ERR,"这是一个业务异常！");
        }*/
        //模拟系统异常，将可能出现的异常进行包装，转换成自定义异常
        /*try {
            int i = 1/0;
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时，请重试",e);
        }*/
        return bookDao.getById(id);
    }

    @Override
    public List<Book> getAll() {
        return bookDao.getAll();
    }
}
